package com.bupt.smart.ui.widget;

import android.content.Context;
import android.location.Address;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

import com.bupt.smart.smart.R;
import com.bupt.smart.ui.adapter.AddressAdapter;
import com.bupt.smart.utils.DensityUtil;

/**
 * Created by HH on 2016/6/20.
 */
public class FlipperListView extends ListView {
    private float orginX;
    private float orginY;
    private float curX;
    private float curY;
    private int deltaX,deltaY;
    View v;
    RelativeLayout layout;
    boolean ishiden;
    int index = -1;
    int lastIndex = -1;
    Context mycontext;
    AddressAdapter myAdapter;
    int dp_layout;  //编辑，删除按钮布局大小，单位dp
    int px_layout;  //编辑，删除按钮布局大小, 单位px
    boolean lock = false;
    //传递所需的context和adapter
    public void setContextAndAdapter(Context context,AddressAdapter addressAdapter,int hidenLength){
        this.dp_layout = hidenLength;
        this.mycontext = context;
        this.myAdapter = addressAdapter;
        px_layout = DensityUtil.dip2px(mycontext,dp_layout);
        Log.i("ishiden","ishiden size:"+addressAdapter.ishiden.length);
    }
    public FlipperListView(Context context) {
        super(context);
    }
    public FlipperListView(Context context, AttributeSet attrs,
                      int defStyle) {
        super(context, attrs, defStyle);
    }
    public FlipperListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    /**
     * 重写该方法，达到使ListView适应ScrollView的效果
     */
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
    /*
    * onTouchEvent
    * 滑屏动作
    * */

    @Override
    public boolean onTouchEvent(MotionEvent e){
        switch(e.getAction()){
            case MotionEvent.ACTION_DOWN:{
                    lock = true; //禁止滚屏
                    for(int i=0;i<myAdapter.ishiden.length;i++)
                        if(!myAdapter.ishiden[i])
                            lastIndex = i;
                    Log.i("action", "ACTION:DOWN from flipper");
                    orginX = e.getX();
                    orginY = e.getY();
                    index = this.pointToPosition((int) orginX, (int) orginY);

                    //判断触碰点在Listview的item上
                    if(index>=0){
                        int temp = index;
                        if((lastIndex!=-1)){
                            temp = index;
                            index = lastIndex;
                        }
                        v = this.getChildAt(index);
                        layout = (RelativeLayout)v.findViewById(R.id.layout_addresslist);
                        if(layout!=null){   //点击直接隐藏
                            layout.scrollTo(0,0);
                            ishiden = true;
                            myAdapter.ishiden[index]=true;
                            lastIndex=-1;
                            index = temp;
                            v = this.getChildAt(index);
                            layout = (RelativeLayout)v.findViewById(R.id.layout_addresslist);
                        }
                    }
                    else
                        Log.i("debug","index:   "+index);


            }break;
            //手指离开item
            case MotionEvent.ACTION_UP:{
                getParent().requestDisallowInterceptTouchEvent(false);
                if(deltaX<0){ //左滑
                    if(ishiden){
                        if(deltaX<(-px_layout/2)&&deltaX>(-px_layout)||deltaX<=(-px_layout)){ //滑动过半，显示按钮
                            if(layout!=null){
                                layout.scrollTo(px_layout,0);
                                myAdapter.ishiden[index] = false;
                                ishiden = false;
                            }
                            else
                                Log.i("error","layout is null from action:up");

                        }
                        else if(deltaX>(-px_layout/2)){ //滑动不到一半，恢复原来位置
                            if(layout!=null){
                                layout.scrollTo(0,0);
                                myAdapter.ishiden[index]=true;
                                lastIndex=-1;
                                ishiden = true;
                            }

                            else
                                Log.i("error","layout is null from action:up");
                        }
                    }
                }

            }break;
            case MotionEvent.ACTION_MOVE: {
                Log.i("action","ACTION:MOVE from flipper");
                lock = true; //禁止上下滚屏
                curX = e.getX();
                curY = e.getY();
                deltaY = (int)(curY-orginY);
                deltaX = (int)(curX-orginX);
                if((deltaX>DensityUtil.dip2px(mycontext,10))&&(deltaY<20))
                {
                    getParent().requestDisallowInterceptTouchEvent(true);

                }
                if(ishiden){
                    if(layout!=null){
                        if(deltaX>=(-px_layout)&&deltaX<0){
                            layout.scrollTo(-deltaX,0);
                        }
                    }
                }
            }
            break;
        }
        return true;
    }
    //AddressAdapter 通过回调该方法隐藏删除/编辑按钮
    public void Address_Del(int position){
        v = this.getChildAt(position);
        layout = (RelativeLayout)v.findViewById(R.id.layout_addresslist);
        layout.scrollTo(0,0);
        myAdapter.ishiden[position] = true;
    }

}
